package org.esgl3d.primitives;

import org.esgl3d.Mesh;
import org.esgl3d.Mesh.Format;
import org.esgl3d.rendering.LegacyGLWrapper;
import org.esgl3d.rendering.Renderer;
import org.esgl3d.rendering.VertexContainer;
import org.esgl3d.rendering.VertexFormat;

public class Box {
	float box[] = new float[] {
			// FRONT
			 0.5f, -0.5f,  0.5f,
			 0.5f,  0.5f,  0.5f,
			-0.5f, -0.5f,  0.5f,
			-0.5f,  0.5f,  0.5f,
			// BACK
			-0.5f, -0.5f, -0.5f,
			-0.5f,  0.5f, -0.5f,
			 0.5f, -0.5f, -0.5f,
			 0.5f,  0.5f, -0.5f,
			// LEFT
			-0.5f, -0.5f,  0.5f,
			-0.5f,  0.5f,  0.5f,
			-0.5f, -0.5f, -0.5f,
			-0.5f,  0.5f, -0.5f,
			// RIGHT
			 0.5f, -0.5f, -0.5f,
			 0.5f,  0.5f, -0.5f,
			 0.5f, -0.5f,  0.5f,
			 0.5f,  0.5f,  0.5f,
			// TOP
			-0.5f,  0.5f,  0.5f,
			 0.5f,  0.5f,  0.5f,
			 -0.5f,  0.5f, -0.5f,
			 0.5f,  0.5f, -0.5f,
			// BOTTOM
			-0.5f, -0.5f,  0.5f,
			-0.5f, -0.5f, -0.5f,
			 0.5f, -0.5f,  0.5f,
			 0.5f, -0.5f, -0.5f,
		};
	
	float texCoords[] = new float[] {
			// FRONT
			 0.0f, 1.0f,
			 0.0f, 0.0f,
			 1.0f, 1.0f,
			 1.0f, 0.0f,
			// BACK
			 1.0f, 0.0f,
			 1.0f, 1.0f,
			 0.0f, 0.0f,
			 0.0f, 1.0f,
			// LEFT
			 1.0f, 0.0f,
			 1.0f, 1.0f,
			 0.0f, 0.0f,
			 0.0f, 1.0f,
			// RIGHT
			 1.0f, 0.0f,
			 1.0f, 1.0f,
			 0.0f, 0.0f,
			 0.0f, 1.0f,
			// TOP
			 0.0f, 0.0f,
			 1.0f, 0.0f,
			 0.0f, 1.0f,
			 1.0f, 1.0f,
			// BOTTOM
			 1.0f, 0.0f,
			 1.0f, 1.0f,
			 0.0f, 0.0f,
			 0.0f, 1.0f
		};
	
	VertexContainer container = null;
	
	private Box(Renderer r) {
		container = r.createVertexContainer(VertexFormat.F_2Texture_3Position, box.length/3);
		//container = r.createVertexContainer(FormatType.F_4Color_3Position, box.length/3);
		LegacyGLWrapper gl = container.getLegacyGLInterface();
		
		gl.glBegin();
		for (int i =0;i<box.length/3;i++) {
			gl.glTexCoord2f(texCoords[i*2], texCoords[i*2+1]);
			gl.glVertex3f(box[i*3]*6, box[i*3+1]*6, box[i*3+2]*6);
		}
		gl.glEnd();
		container.synchronize();
	}
	
	
	public Mesh getMesh() {
		Mesh m = new Mesh();
		m.setVertices(container, Format.TriangleStrip);
		return m;
	}
	
	public static Mesh createTexturedBox(Renderer r) {
		return new Box(r).getMesh();
	}
	
	public static Mesh createTexturedBoxWithNormals(Renderer r) {
		return new Box(r).getMesh();
	}
	
}
